//
//  ContinuePlanViewController.m
//  IpadTestB
//
//  Created by edianzu on 2017/7/11.
//  Copyright © 2017年 ruilai. All rights reserved.
//

#import "ContinuePlanViewController.h"
#import "AddChildViewController.h"
#import "BeginUnSaleTestViewController.h"
@interface ContinuePlanViewController ()
@property(nonatomic,assign)NSInteger joinProNumber;//参与计划的人数
@property(nonatomic,strong)NSMutableDictionary * allDataDictionary;
@end

@implementation ContinuePlanViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"继续测试计划";
    self.view.backgroundColor = UIColorFromRGB(241, 246, 248);
    [self.navigationController.navigationBar setTitleTextAttributes:
     @{NSFontAttributeName:[UIFont boldSystemFontOfSize:23*AutoScaleHorizontal],
       NSForegroundColorAttributeName:[UIColor whiteColor]}];
    ////////////////左按钮
    UIButton * leftBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    leftBtn.frame = CGRectMake(0, 0, 35,35);
    [leftBtn setBackgroundImage:[UIImage imageNamed:@"navBack"] forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(leftBarBtnClicked) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem * leftBarBtn = [[UIBarButtonItem alloc]initWithCustomView:leftBtn];;
    //创建UIBarButtonSystemItemFixedSpace
    UIBarButtonItem * spaceItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    //将宽度设为负值
    spaceItem.width = 15;
    //将两个BarButtonItem都返回给NavigationItem
    self.navigationItem.leftBarButtonItems = @[spaceItem,leftBarBtn];
//    [self getData];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self getData];
}

-(void)getData{
    NSUserDefaults * user = [NSUserDefaults standardUserDefaults];
    //获取全部数据
    NSString* str = [user objectForKey:@"AllData"];
    //    NSLog(@"eee===%@",str);
    NSData *jsonData = [str dataUsingEncoding:NSUTF8StringEncoding];
    _allDataDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:nil];
    //循环遍历看有多少人参加了现有的计划
    NSMutableArray * testArray = [NSMutableArray arrayWithArray: [_allDataDictionary objectForKey:@"TestResult"]];
    NSMutableArray * childArray = [NSMutableArray arrayWithArray:[_allDataDictionary objectForKey:@"ChildList"]];
    
    
        _joinProNumber = 0;
//        for (NSMutableDictionary * childDic in childArray) {
    
            
            for (NSDictionary * dict in testArray) {
                NSDictionary * masterdic = [dict objectForKey:@"master"];
                
//                if ([[childDic objectForKey:@"Id"]isEqual:[masterdic objectForKey:@"ChildId"]]) {
                
                    if ([[masterdic objectForKey:@"ProgramId"]isEqual:self.ownModel.Id]) {
                        _joinProNumber += 1;
                    }
                }
//            }
//    }
     self.testNumberLabel.text = @"";
    [user synchronize];
    [self creatUI];
}

-(void)leftBarBtnClicked{
    NSLog(@"左按钮");
    [self.navigationController popViewControllerAnimated:YES];
}


-(void)creatUI{
    
    self.whiteView = [[UIView alloc]init];
    self.whiteView.layer.cornerRadius = 10;
    self.whiteView.layer.masksToBounds = YES;
    self.whiteView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.whiteView];
    [self.whiteView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view.mas_top).with.offset(25*AutoScaleVertical);
        make.left.mas_equalTo(self.view.mas_left).with.offset(75*AutoScaleHorizontal);
        make.right.mas_equalTo(self.view.mas_right).with.offset(-75*AutoScaleHorizontal);
        make.height.mas_equalTo(125*AutoScaleVertical);
    }];
    
    self.planNameLabel = [[UILabel alloc]init];
    self.planNameLabel.textColor = [UIColor blackColor];
    self.planNameLabel.font = [UIFont boldSystemFontOfSize:20*AutoScaleHorizontal];
    self.planNameLabel.text = self.ownModel.Title;
    [self.whiteView addSubview:self.planNameLabel];
    [self.planNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.whiteView.mas_left).with.offset(45*AutoScaleHorizontal);
        make.top.mas_equalTo(self.whiteView.mas_top).with.offset(30*AutoScaleVertical);
        make.size.mas_equalTo(CGSizeMake(700*AutoScaleHorizontal, 20*AutoScaleVertical));
    }];
    
    self.teacherNameLabel = [[UILabel alloc]init];
    self.teacherNameLabel.textColor = [UIColor blackColor];
    self.teacherNameLabel.font = [UIFont systemFontOfSize:20*AutoScaleHorizontal];
    self.teacherNameLabel.text = _ownModel.CreateTeacher;
    [self.whiteView addSubview:self.teacherNameLabel];
    [self.teacherNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.whiteView.mas_left).with.offset(45*AutoScaleHorizontal);
        make.top.mas_equalTo(self.planNameLabel.mas_bottom).with.offset(25*AutoScaleVertical);
        make.size.mas_equalTo(CGSizeMake(250*AutoScaleHorizontal, 20*AutoScaleVertical));
    }];
    
    self.timeLabel = [[UILabel alloc]init];
    self.timeLabel.textColor = [UIColor blackColor];
    self.timeLabel.font = [UIFont systemFontOfSize:20*AutoScaleHorizontal];
    self.timeLabel.text = [NSString stringWithFormat:@"创建时间:%@",[_ownModel.CreateTime substringWithRange:NSMakeRange(0, 10)]];
    [self.whiteView addSubview:self.timeLabel];
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.teacherNameLabel.mas_right);
        make.top.mas_equalTo(self.teacherNameLabel.mas_top);
        make.size.mas_equalTo(CGSizeMake(300*AutoScaleHorizontal, 20*AutoScaleVertical));
    }];
    
    self.addChildBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.addChildBtn addTarget:self action:@selector(touchAdd) forControlEvents:UIControlEventTouchUpInside];
    [self.addChildBtn setImage:[UIImage imageNamed:@"addChildBtnS"] forState:UIControlStateNormal];
    [self.view addSubview: self.addChildBtn];
    [self.addChildBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.whiteView.mas_bottom).with.offset(100*AutoScaleVertical);
        make.centerX.mas_equalTo(self.view.mas_centerX);
        make.size.mas_equalTo(CGSizeMake(450*AutoScaleHorizontal, 150*AutoScaleVertical));
    }];
    
    self.testNumberLabel = [[UILabel alloc]init];
    self.testNumberLabel.textColor = UIColorFromRGB(211, 93, 59);
    [self.testNumberLabel setTextAlignment:NSTextAlignmentCenter];
    self.testNumberLabel.font = [UIFont systemFontOfSize:20*AutoScaleHorizontal];
    self.testNumberLabel.text = [NSString stringWithFormat:@"测试人员:%d人",self.joinProNumber];
    [self.view addSubview:self.testNumberLabel];
    [self.testNumberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view.mas_centerX);
        make.bottom.mas_equalTo(self.addChildBtn.mas_top).with.offset(-20*AutoScaleVertical);
        make.size.mas_equalTo(CGSizeMake(300*AutoScaleHorizontal, 20*AutoScaleVertical));
    }];
    
    self.beginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.beginBtn addTarget:self action:@selector(touchBegin) forControlEvents:UIControlEventTouchUpInside];
    if (self.joinProNumber == 0) {
        [self.beginBtn setTitle:@"开始测试" forState:UIControlStateNormal];
    }else{
        [self.beginBtn setTitle:@"继续测试" forState:UIControlStateNormal];
    }
    
    self.beginBtn.backgroundColor = UIColorFromRGB(26, 167, 157);
    self.beginBtn.titleLabel.font = [UIFont boldSystemFontOfSize:25*AutoScaleHorizontal];
    [self.beginBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.beginBtn.layer.cornerRadius =10;
    self.beginBtn.layer.masksToBounds = YES;
    [self.view addSubview: self.beginBtn];
    [self.beginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.view.mas_bottom).with.offset(-120*AutoScaleVertical);
        make.centerX.mas_equalTo(self.view.mas_centerX);
        make.size.mas_equalTo(CGSizeMake(200*AutoScaleHorizontal, 80*AutoScaleVertical));
    }];
}

-(void)touchAdd{
    AddChildViewController * vc = [[AddChildViewController alloc]init];
    vc.isFromSale = @"0";
    vc.ownModel = self.ownModel;
    [self.navigationController pushViewController:vc animated:YES];
    
}

-(void)touchBegin{
    BeginUnSaleTestViewController * btvc = [[BeginUnSaleTestViewController alloc]init];
    btvc.ownPlanModel =self.ownModel;
    [self.navigationController pushViewController:btvc animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
